package com.gjh.xiaokabang2code.controller;

import com.gjh.xiaokabang2code.common.*;
import com.gjh.xiaokabang2code.model.request.CloseRt;
import com.gjh.xiaokabang2code.model.request.SearchLoserRt;
import com.gjh.xiaokabang2code.model.response.FinderRs;
import com.gjh.xiaokabang2code.model.response.LoginRs;
import com.gjh.xiaokabang2code.model.response.LoserRs;
import com.gjh.xiaokabang2code.service.SearchFinderService;
import com.gjh.xiaokabang2code.service.SearchLoserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/search")
@CrossOrigin(origins = "${cors.origins}", allowCredentials = "true")
public class SearchController {

    @Value("${cors.origins}")
    private String allowedOrigins;
    @Resource
    private SearchLoserService searchLoserService;

    @Resource
    private SearchFinderService searchFinderService;

    @PostMapping("/loser")
    public BaseResponse<LoserRs> searchLoser(@RequestBody SearchLoserRt searchLoserRt, HttpServletRequest httpServletRequest) throws Exception {
        LoginRs  loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        LoserRs loserRs = searchLoserService.searchLoser(searchLoserRt, loginUser);
        if (loserRs == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回数据为空");
        }
        return ResultUtils.success(loserRs);
    }

    @GetMapping("/lose")
    public BaseResponse<String> loseToList(HttpServletRequest httpServletRequest) throws Exception {
        LoginRs  loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        String result = searchFinderService.loseToList(loginUser);
        if (result == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回数据为空");
        }
        return ResultUtils.success(result);
    }

    @GetMapping("/finder")
    public BaseResponse<FinderRs> searchFinder(HttpServletRequest httpServletRequest) throws Exception {
        LoginRs  loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        FinderRs finderRs = searchFinderService.searchFinder(loginUser);
        if (finderRs == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回数据为空");
        }
        return ResultUtils.success(finderRs);
    }

    @PostMapping("/close")
    public BaseResponse<Boolean> closeSearchFinder(@RequestBody CloseRt closeRt, HttpServletRequest httpServletRequest) throws Exception {
        LoginRs  loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        Boolean result= searchFinderService.closeSearchFinder(closeRt, loginUser);
        return ResultUtils.success(result);
    }


}
